package com.hanthink.system.service.impl.delivery;

import java.util.List;

import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.delivery.DeliveryProductionDeliveryPlan;
import com.hanthink.system.domain.logistics.ProductionDeliveryPlan;
import com.hanthink.system.domain.manhour.ProductionPlanInShift;
import com.hanthink.system.mapper.delivery.DeliveryProductionDeliveryPlanMapper;
import com.hanthink.system.service.delivery.IDeliveryProductionDeliveryPlanService;
import com.hanthink.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hanthink.system.mapper.delivery.DeliveryProductionDeliveryPlanMapper;
import com.hanthink.system.domain.delivery.DeliveryProductionDeliveryPlan;
import com.hanthink.system.service.delivery.IDeliveryProductionDeliveryPlanService;

/**
 * 运输计划管理Service业务层处理
 * 
 * @author hanthink
 * @date 2022-09-05
 */
@Service
public class DeliveryProductionDeliveryPlanServiceImpl implements IDeliveryProductionDeliveryPlanService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    private DeliveryProductionDeliveryPlanMapper productionDeliveryPlanMapper;

    /**
     * 查询运输计划管理
     * 
     * @param id 运输计划管理ID
     * @return 运输计划管理
     */
    @Override
    public DeliveryProductionDeliveryPlan selectProductionDeliveryPlanById(Long id)
    {
        return productionDeliveryPlanMapper.selectProductionDeliveryPlanById(id);
    }

    /**
     * 查询运输计划管理列表
     * 
     * @param productionDeliveryPlan 运输计划管理
     * @return 运输计划管理
     */
    @Override
    public List<DeliveryProductionDeliveryPlan> selectProductionDeliveryPlanList(DeliveryProductionDeliveryPlan productionDeliveryPlan)
    {
        return productionDeliveryPlanMapper.selectProductionDeliveryPlanList(productionDeliveryPlan);
    }

    /**
     * 新增运输计划管理
     * 
     * @param productionDeliveryPlan 运输计划管理
     * @return 结果
     */
    @Override
    public int insertProductionDeliveryPlan(DeliveryProductionDeliveryPlan productionDeliveryPlan)
    {
        return productionDeliveryPlanMapper.insertProductionDeliveryPlan(productionDeliveryPlan);
    }

    /**
     * 修改运输计划管理
     * 
     * @param productionDeliveryPlan 运输计划管理
     * @return 结果
     */
    @Override
    public int updateProductionDeliveryPlan(DeliveryProductionDeliveryPlan productionDeliveryPlan)
    {
        return productionDeliveryPlanMapper.updateProductionDeliveryPlan(productionDeliveryPlan);
    }

    /**
     * 批量删除运输计划管理
     * 
     * @param ids 需要删除的运输计划管理ID
     * @return 结果
     */
    @Override
    public int deleteProductionDeliveryPlanByIds(Long[] ids)
    {
        return productionDeliveryPlanMapper.deleteProductionDeliveryPlanByIds(ids);
    }

    /**
     * 删除运输计划管理信息
     * 
     * @param id 运输计划管理ID
     * @return 结果
     */
    @Override
    public int deleteProductionDeliveryPlanById(Long id)
    {
        return productionDeliveryPlanMapper.deleteProductionDeliveryPlanById(id);
    }

    /**
     * 导入数据
     *
     * @param PrdPlanItemList     数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importDeliveryPlan(List<DeliveryProductionDeliveryPlan> PrdPlanItemList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(PrdPlanItemList) || PrdPlanItemList.size() == 0) {
            throw new CustomException("导入的实际用工数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int currentRowNum = 0;
        for (DeliveryProductionDeliveryPlan prdPlanItem : PrdPlanItemList) {
            currentRowNum++;
            try {
                DeliveryProductionDeliveryPlan items = productionDeliveryPlanMapper.selectProductionDeliveryPlanByKeys(prdPlanItem) ;
                if (StringUtils.isNull(items)) {
                    prdPlanItem.setCreateBy(operName);
                    //1.过去的数据是否能修改?(原则上过去计划数据不能修改）
                    //2.修改为可以覆盖数据

                    this.insertProductionDeliveryPlan(prdPlanItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、运输计划数据 工厂：" + prdPlanItem.getFactoryid() + " 运输日期：" + prdPlanItem.getDeliverydatetime() +" 客户："+ prdPlanItem.getCustomergroup() + " 序号：" + prdPlanItem.getDeliveryindex() + " 导入成功！");
                } else if (isUpdateSupport) {
                    prdPlanItem.setUpdateBy(operName);
                    // prdPlanItem.setId(items.getId());
                    this.updateProductionDeliveryPlan(prdPlanItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、运输计划数据 " +  prdPlanItem.getFactoryid() + " 运输日期：" + prdPlanItem.getDeliverydatetime() +" 客户："+ prdPlanItem.getCustomergroup() + " 序号：" + prdPlanItem.getDeliveryindex() + " 更新成功");
                    continue;
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、运输计划数据 " + prdPlanItem.getFactoryid() + " 运输日期：" + prdPlanItem.getDeliverydatetime() +" 客户："+ prdPlanItem.getCustomergroup() + " 序号：" + prdPlanItem.getDeliveryindex() + " 已存在");
                    continue;
                }
            }catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、运输计划数据" + prdPlanItem.getFactoryid() + " 运输日期：" + prdPlanItem.getDeliverydatetime() +" 客户："+ prdPlanItem.getCustomergroup() + " 序号：" + prdPlanItem.getDeliveryindex() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
